/**
 * @author Linrui
 * @since 2011-05-01
 * 用户个人安全控制，修改帐号密码
 */
package com.qeado.user.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.qeado.user.model.User;
import com.qeado.user.service.UserService;

@Scope("request")
@Controller
public class SecurityManageAction extends ActionSupport {

	@Autowired
	private UserService userService;
	
	private String password; // 旧密码
	private String password_new; // 新密码
	private String confirm; // // 新密码确认
	private String returnInfo; // 因错误返回的信息

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPassword_new() {
		return password_new;
	}

	public void setPassword_new(String passwordNew) {
		password_new = passwordNew;
	}

	public String getConfirm() {
		return confirm;
	}

	public void setConfirm(String confirm) {
		this.confirm = confirm;
	}

	public String getReturnInfo() {
		return returnInfo;
	}

	public void setReturnInfo(String returnInfo) {
		this.returnInfo = returnInfo;
	}

	@Override
	public String execute() throws Exception {
		return super.execute();
	}

	public String updatePassword() throws Exception{
		if( password_new != null && !"".equals(password_new) && password_new.equals(confirm)) {
			String id = ((User)ActionContext.getContext().getSession().get("login_user")).getId();
			if( userService.checkPassword(id, password) ) {
				if( userService.updatePassword(id, password_new) ) {
					returnInfo = "密码修改成功";
					return "Update Success";
				} else {
					returnInfo = "密码修改失败";
					return "Update Fail";
				}
			} else {
				returnInfo = "您输入的旧密码不正确";
				return "Update Fail";
			}
		} else {
			returnInfo = "两次密码输入不同";
			return "Update Fail";
		}
	}
}
